<template>
    <!-- 组件的模板 -->
    <div class="page attendance-set show-footerBtn" :style="isIphoneX?'padding-bottom: 89px;':''">        
        <div class="weui-cells option-set">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="addPosition">
                <div class="weui-cell__bd">
                    <p>考勤地点</p>
                </div>
                <div class="weui-cell__ft" v-if="taskObj.positon_info.length != 0">共{{taskObj.positon_info.length}}个</div>
                <div class="weui-cell__ft" v-else></div>

            </a>                             
        </div>
        <div class="weui-cells__tip">用户必须在考勤地点附近打卡，否则记为异常</div>         

        <div class="weui-cells option-set" style="margin-top:0px">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectField">
                <div class="weui-cell__bd">
                    <p>考勤填写项</p>
                </div>
                <div class="weui-cell__ft">{{require_word}}</div>
            </a>                             
           <!--  <a class="weui-cell weui-cell_access" href="javascript:;" @click="changeTuneDistance">
                <div class="weui-cell__bd">
                    <p>地点微调</p>
                </div>
                <div class="weui-cell__ft" v-if="taskObj.adjust_distance == '关闭'">{{taskObj.adjust_distance}}</div>                
                <div class="weui-cell__ft" v-else>{{taskObj.adjust_distance}}米</div>                
            </a> -->
            
            <!-- <a class="weui-cell weui-cell_access" href="javascript:;" @click="isOpenchange()">
                <div class="weui-cell__bd">
                    <p>其他设置项</p>
                </div>
                <div class="weui-cell__ft up" v-if="isOpen"></div>
                <div class="weui-cell__ft down" v-else></div>
            </a> --> <!--这期暂时不做，但是之后会用-->
        </div>
        <div class="weui-cells__tip">可设置用户打卡时需要填写的内容</div>         
        <div class="weui-cells option-set" style="margin-top:0px">            
           <!--  <a class="weui-cell weui-cell_access" href="javascript:;" @click="changeTuneDistance">
                <div class="weui-cell__bd">
                    <p>地点微调</p>
                </div>
                <div class="weui-cell__ft" v-if="taskObj.adjust_distance == '关闭'">{{taskObj.adjust_distance}}</div>                
                <div class="weui-cell__ft" v-else>{{taskObj.adjust_distance}}米</div>                
            </a> -->
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectNoticeType">
                <div class="weui-cell__bd">
                    <p>考勤通知</p>
                </div>
                <div class="weui-cell__ft">{{noticeMessage}}</div>
            </a>
            
            <!-- <a class="weui-cell weui-cell_access" href="javascript:;" @click="isOpenchange()">
                <div class="weui-cell__bd">
                    <p>其他设置项</p>
                </div>
                <div class="weui-cell__ft up" v-if="isOpen"></div>
                <div class="weui-cell__ft down" v-else></div>
            </a> --> <!--这期暂时不做，但是之后会用-->
        </div>
        <!-- <div class="weui-cells sub-option" v-if="isOpen">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectNoticeType">
                <div class="weui-cell__bd">
                    <p>考勤通知</p>
                </div>
                <div class="weui-cell__ft">{{noticeMessage}}</div>
            </a>
            <div class="weui-cell weui-cell_switch weui-cells_form">
                <div class="weui-cell__bd">是否允许地点微调</div>
                <div class="weui-cell__ft">
                    <input class="weui-switch" type="checkbox" checked="checked" v-model="position_tune1">
                </div>
            </div>
            <a class="weui-cell weui-cell_access" href="javascript:;" v-if="position_tune1" @click="changeTuneDistance">
                <div class="weui-cell__bd">
                    <p>允许微调范围</p>
                </div>
                <div class="weui-cell__ft">{{tune_distance}}米</div>                
            </a>
        </div>   -->     <!--这期暂时不做，但是之后会用--> 
        <div class="btn-box bg-color fixed" :style="isIphoneX?'padding-bottom: 35px;':''" v-if="classesType == 1 || classesType == 0">
            <a href="javascript:;" class="weui-btn weui-btn_primary" @click="submit">保存</a>
        </div>
        <div class="btn-box bg-color fixed" :style="isIphoneX?'padding-bottom: 35px;':''" v-else>
            <a href="javascript:;" class="weui-btn weui-btn_primary" @click="scheduleEdit">保存并开始排班</a>
        </div>
    </div>
</template>

<script>
    let pageDate = {},
        taskId = '',
        getInfoData = '';
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import widget from 'gapi/widget'
    import clientUi from 'gapi/clientUi.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                dataChane: false,
                position_tune1: 0,//是否允许地点微调 0：不允许 1：允许，不传或空表示不允许
                
                // isOpen:false,  // 判断展开项，这期暂时不做，之后会做
                taskObj:{
                    position:[],   // 考勤位置
                    adjust_distance: 500,//地点微调范围默认200
                    // require_field:{photo:0,position:1,remark:0},//考勤必填项 0:选中／1:不选中 
                    require_field:[
                        {
                            "id": '',
                            "type": "Location",
                            "name": "位置",
                            "info": {
                                "title": "位置",
                                "value": {
                                    "lat": "",
                                    "lng": "",
                                    "addr": "",
                                    "addr_name":""
                                },
                                "tune_distance":500,
                                "tip": "",
                                "required": 1
                            },
                            "order": 1
                        }
                    ],
                    photoImg:{
                        img_thumb_url:[],
                        img_url:[]
                    },
                    notice_type:0,   // 考勤通知
                    positon_info: [], //考勤地点
                    classesType:0,//排版类型
                },
                checkparticapant: 1,//是否检测考勤人员
            }
        },
        computed: {
            noticeMessage(){
                if(this.taskObj.notice_type==0){
                    return '提交不需要通知'
                }else if(this.taskObj.notice_type==1){ 
                    return '仅考勤状态异常时通知'
                }else if(this.taskObj.notice_type==2){
                    return '提交考勤即通知'
                }
            },
            requireFieldMessage(){
                let res = ''
                if(this.taskObj.require_field.photo==1){
                    res = '拍照'
                }
                if(this.taskObj.require_field.position==1){
                    res = res?res+'、位置':'位置'
                }
                if(this.taskObj.require_field.remark==1){
                    res = res?res+'、备注':'备注'
                }
                return res
            },
            require_word() {
                let str = ''
                this.taskObj.require_field.forEach((obj) => {
                    str += `、${obj.info.title}`
                })
                return str.substr(1)
            },
            isIphoneX: function(){
                return util.isIphoneX()
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.classesType = util.getUrddataString('classes_type')           

                cache.getCache('KqCacheMessge',(data)=>{
                    if(data){
                        pageDate.project_id = data.project_id || util.getUrddataString('project_id')
                        pageDate.team_id = data.team_id || util.getUrddataString('team_id')
                        pageDate.which_gback = data.which_gback || util.getUrddataString('which_gback')
                    }else{
                        pageDate.project_id = util.getUrddataString('project_id')
                        pageDate.team_id = util.getUrddataString('team_id')
                        pageDate.which_gback = util.getUrddataString('which_gback')

                    }
                    cache.deleteCache('getDate')
                 })

            },            
            addPosition(){
                cache.setCache('KaoqinAddPosition',this.taskObj.positon_info,60*60*24)
                window.location.href="dmsaas://KaoqinAddPosition"
            },
            //删除办公地点
            deletePosition(index){
                for(var i = 0 ;i< this.taskObj.position.length;i++){
                    if(i>=index){
                        this.taskObj.position[i] = this.taskObj.position[i+1]
                    }
                }
                this.taskObj.position.pop()
            },
            //考勤必填项
            selectField(){
                // let imageObj = {
                //     require_field : this.taskObj.require_field,
                //     img_url : this.taskObj.photoImg.img_url,  // 原图
                //     img_thumb_url : this.taskObj.photoImg.img_thumb_url  // 缩略图
                // }
                // console.log(imageObj)
                // cache.setMemoryCache('requireField', imageObj)
                // window.location.href="dmsaas://KaoqinSetRequirefield"

                cache.setCache('designInfo', this.taskObj.require_field, 10000)
                location.href = `dmsaas://SetupRequire`
            },
            // 其他设置项展开或收起----这期暂时不做，之后会做
            // isOpenchange (){
            //     this.isOpen = this.isOpen == false ? true : false
            // },   
            selectNoticeType(){
                cache.setMemoryCache('noticeType', {key:this.taskObj.notice_type})
                window.location.href="dmsaas://KaoqinSetNoticetype?" + `project_id=${pageDate.project_id}`
            },
            // 地点微调
            changeTuneDistance(){
                let tune_distances = {
                    data1: ['关闭',100,200,500,1000],
                }
                let index = tune_distances.data1.indexOf(this.taskObj.adjust_distance)
                let select = [index]
                clientUi.showPicker(tune_distances, select, "请选择微调范围", (data) => {
                    console.log(data)
                    if(data&&data.data1){
                       this.taskObj.adjust_distance = tune_distances.data1[data.data1] 
                    }
                })
            },
            // 更新数据
            updateDate(data){
                if(data){
                    let key = data.key
                    let value = data.value
                    switch(key){ 
                            //更新考勤位置信息
                        case 'position':
                            this.taskObj.position.push(value)
                            break
                        case 'requireField':
                            this.taskObj.require_field = data.require_field
                            break
                        case 'noticeType':
                            this.taskObj.notice_type = value
                            break
                        case 'KaoqinAddPosition':
                            this.taskObj.positon_info = value //获取考勤地点数量
                            break
                        default:
                            break
                    }
                }
            },
            // 验证必填项，为空时，提示
            checkDate (){
                // if(this.taskObj.positon_info.length == 0){
                //     widget.toast('请设置考勤地点')
                //     return false
                // }
                if(this.taskObj.require_field.photo == 0 && this.taskObj.require_field.position == 0 && this.taskObj.require_field.remark == 0){
                    widget.toast('请选择必填项')
                    return false
                }
                return true
            },
            submit (){
                if(!this.checkDate()){
                    return
                }
                // 判断当前选择排班方式为固定时间还是弹性时间，固定时间为0，弹性时间为1
                this.taskObj.work_type = this.fixedTime == true ? 0 : 1  
                // 判断当前选择考勤日期为按天设置还是按周设置，按天设置为0，按周设置为1
                this.taskObj.work_date_type = this.daySetting == true ? 0 : 1
                let getSubmitDate = this.getSubmitDate()
                util.ajax({
                    url: '/attendance/attendance_task/add',
                    type:'POST',
                    data: getSubmitDate,
                    success: (res) => {
                        console.log(res)
                        if(res&&res.errno == 0){                            
                            //埋点
                            let statisticsLog_data = {
                                event_id: '新建考勤规则-保存',
                                // event_label: data.event_label,(预留，现在不用传此参数)
                                event_param: {
                                    ca_from: 'save',
                                    project_id: pageDate.project_id,
                                }
                            }
                            KCApiApp.statisticsLog(statisticsLog_data)

                            cache.deleteCache('KaoqinClassesSet')
                            cache.deleteCache('KaoqinAddPosition')                            
                            if(getInfoData.which_gback && getInfoData.which_gback == 1){
                                cache.getMemoryCache('employeekqViewID', (data) => {
                                    if(data){
                                        commonPage.goBack(1, {'key':'yy_kqtaskset'}, data.employeekqViewID)
                                    }
                                })
                            }else{
                                cache.getMemoryCache('editKqViewID', (data) => {
                                    if(data){
                                        commonPage.goBack(1, {'key':'yy_kqtaskset'}, data.editKqViewID)
                                    }
                                })
                            }                            
                        }else if(res.errno == 21039){
                            clientUi.showDialog('提醒', '考勤小组、人员与其他考勤规则冲突，确定将其拉至该考勤规则', (status) => {
                                if(status.status == 1){
                                   this.checkparticapant = 0
                                   let getSubmitDate = this.getSubmitDate()
                                   this.submit();
                                }
                            }, '确认', '取消')
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }else{
                            this.noMoreClick = true
                            widget.toast('提交失败')
                        }
                    },
                    error: (xhr, status) => {
                        console.log(xhr)
                        this.noMoreClick = true
                        widget.toast('网络连接失败，请检查网络')
                    },
                    noNetwork: () => {
                        this.noMoreClick = true
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            scheduleEdit (){
                if(!this.checkDate()){
                    return
                }
                // 判断当前选择排班方式为固定时间还是弹性时间，固定时间为0，弹性时间为1
                this.taskObj.work_type = this.fixedTime == true ? 0 : 1  
                // 判断当前选择考勤日期为按天设置还是按周设置，按天设置为0，按周设置为1
                this.taskObj.work_date_type = this.daySetting == true ? 0 : 1
                let getSubmitDate = this.getSubmitDate()
                util.ajax({
                    url: '/attendance/attendance_task/add',
                    type:'POST',
                    data: getSubmitDate,
                    success: (res) => {
                        if(res&&res.errno == 0){
                            //埋点
                            let statisticsLog_data = {
                                event_id: '新建考勤规则-保存',
                                // event_label: data.event_label,(预留，现在不用传此参数)
                                event_param: {
                                    ca_from: 'save',
                                    project_id: pageDate.project_id,
                                }
                            }
                            // alert(JSON.stringify(statisticsLog_data))
                            KCApiApp.statisticsLog(statisticsLog_data)
                            let param_task_id = res.data.task_id
                            cache.deleteCache('KaoqinClassesSet')
                            cache.deleteCache('KaoqinAddPosition')
                            window.location.href="dmsaas://KaoqinScheduleEdit?"+`task_id=${param_task_id}`
                        }else if(res.errno == 21039){
                            clientUi.showDialog('提醒', '考勤小组、人员与其他考勤规则冲突，确定将其拉至该考勤规则', (status) => {
                                // alert(status.status)
                                if(status.status == 1){
                                   this.checkparticapant = 0
                                   let getSubmitDate = this.getSubmitDate()
                                   this.submit();
                                }
                            }, '确认', '取消')
                        }else if(res.errmsg){
                            widget.toast(res.errmsg)
                        }else{
                            this.noMoreClick = true
                            widget.toast('提交失败')
                        }
                    },
                    error: (xhr, status) => {
                        console.log(xhr)
                        this.noMoreClick = true
                        widget.toast('网络连接失败，请检查网络')
                    },
                    noNetwork: () => {
                        this.noMoreClick = true
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
               
            },
            getSubmitDate(){
                // let pushImg = []
                // getInfoData.photoImg.img_thumb_url.forEach( (item) => {
                //     pushImg.push(item.replace(util.baseImageDomain,''))
                // })       
                let data = {
                    team_id: getInfoData.team_id,
                    project_id: getInfoData.project_id,
                    name: getInfoData.name,
                    participant_group: getInfoData.participant_group,
                    participant_user: getInfoData.participant_user,
                    position: this.taskObj.positon_info,
                    work_type: this.classesType, //排班方式
                    work_time_range: getInfoData.work_time_range,  // 固定时间
                    work_time_length: getInfoData.work_time_length,  // 弹性时间
                    schedule_list: getInfoData.schedule_list,
                    work_date_type: 1,   // 考勤日期方式
                    work_date: getInfoData.work_date,   // 按天设置
                    work_week: getInfoData.work_week,   //按周设置
                    require_field: this.taskObj.require_field,
                    notice_type: this.taskObj.notice_type,
                    checkparticapant: this.checkparticapant,
                    today_begin: getInfoData.today_begin
                    // adjust_distance:getInfoData.adjust_distance,
                    // examples_img:JSON.stringify(pushImg)
                }
                return data
            },
        },
        created(){
            this.init();
            window.loadData = this.updateDate
            cache.getCache('taskObjInfo', (data) => {
                getInfoData = data
            })
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance-set.css"></style>



